#include "list.h"
#include <stdlib.h>
ListHeadTepedef *create_lsit_head(void){
    ListHeadTepedef *new_head=(ListHeadTepedef *)malloc(sizeof(ListHeadTepedef));
    new_head->element_size=0;
    new_head->head=NULL;
    new_head->tail=NULL;
    return new_head;
}

void push_back(ListHeadTepedef *list,Lelement new_element){
    if(list==NULL){
        return ;
    }
    LinkListTypedef *new=(LinkListTypedef *)malloc(sizeof(LinkListTypedef));
    new->element=new_element;
    new->next=NULL;
    LinkListTypedef *now=NULL;

    if(list->element_size==0){
        list->head=new;
        list->tail=new;
    }else{
        now=list->tail;
        now->next=new;
        list->tail=new;
    }
    list->element_size++;
}

void push_head(ListHeadTepedef *list,Lelement new_element){
    if(list==NULL){
        return ;
    }
    LinkListTypedef *new=(LinkListTypedef *)malloc(sizeof(LinkListTypedef));
    new->element=new_element;
    new->next=NULL;
    LinkListTypedef *now=NULL;

    if(list->element_size==0){
        list->head=new;
        list->tail=new;
    }else{
        now=list->head;
        list->head=new;
        new->next=now;
    }
    list->element_size++;
}

LinkListTypedef* pop_back(ListHeadTepedef *list){
    if(list==NULL || list->element_size==0){
        return NULL;
    }
    LinkListTypedef *now=list->head;
    LinkListTypedef *new=NULL;
    while(now->next!=list->tail){
        if (list->element_size==1){
            break;
        }
        now=now->next;
    }
    
    if(list->element_size==1){
        list->head=NULL;
        list->tail=NULL;
        new=now;
    }else{
        new=list->tail;
        list->tail=now;
        now->next=NULL;
    }
    list->element_size--;
    return new;
}

LinkListTypedef* pop_head(ListHeadTepedef *list){
    if(list==NULL || list->element_size==0){
        return NULL;
    }
    LinkListTypedef *new=NULL;
    if(list->element_size==1){
        new=list->head;
        list->head=NULL;
        list->tail=NULL;
    }else{
        new=list->head;
        list->head=list->head->next;
        new->next=NULL;
    }
    list->element_size--;
    return new;
}

char Find_ListElement(ListHeadTepedef *list,Lelement find_element){
    if(list==NULL || list->element_size==0){
        return ERROR;
    }
    LinkListTypedef*now=list->head;
    int pos=0;
    while (now!=NULL){
        if(now->element==find_element){
            return pos;
        }
        now=now->next;
        pos++;
    }
    return ERROR;
}

LinkListTypedef* Get_SingleListElement(ListHeadTepedef *list,int element_pos){
    if(element_pos>list->element_size-1 || element_pos<0){
        printf("get single data error\r\n");
        return NULL;
    }
    int i = 0;
    LinkListTypedef* return_val;
    LinkListTypedef *now=list->head;
    for(i = 0; i < list->element_size;i++){
        if(i==element_pos){
            return_val->element=now->element;
            break;
        }
        now=now->next;
    }
    return return_val;
}

u8 IsEmpty(ListHeadTepedef *list){
    if(list->element_size==0){
        return TRUE;
    }
    return FALSE;
}

void list_flipping(ListHeadTepedef*list){
    if(list->head==list->tail||list->head->next==NULL){
        return ;
    }
    LinkListTypedef*now=list->head;
    list->head=list->head->next;
    list_flipping(list);
    list->tail->next=now;
    list->tail=now;
    now->next=NULL;
}

void list_print(ListHeadTepedef *list){
    if(list==NULL || list->element_size==0){
        return ;
    }
    LinkListTypedef *now=list->head;
    while (now!=NULL){
        printf("%s ",now->element);
        now=now->next;
    }
    printf("\n");
}

void destroy_list(ListHeadTepedef *list){
    if(list==NULL){
        return ;
    }
    LinkListTypedef *ptr=list->head;
    LinkListTypedef *next=NULL;
    int i = 0;
    for(i = 0; i < list->element_size;i++){
        next=ptr;
        ptr=ptr->next;
        free(next);
        next=NULL;
    }
    list->element_size=0;
    free(list);
    list=NULL;
}